package com.developer.weatherdata.util;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public final class Util {

	private static Util INSTANCE;

	private final Context context;

	private Util(Context context) {
		this.context = context;
	}

	public static Util getInstance(Context context) {
		if (INSTANCE == null) {
			INSTANCE = new Util(context);
		}
		return INSTANCE;
	}

	public boolean isInternetAvailable() {
		ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo netInfo = connectivityManager.getActiveNetworkInfo();
		return (netInfo != null) && (netInfo.isConnected());
	}

	public Bitmap downloadImage(String url) {
		Bitmap bitmap = null;
		InputStream inputStream = null;
		try {
			inputStream = new URL(url).openStream();
			bitmap = BitmapFactory.decodeStream(inputStream);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (inputStream != null) inputStream.close();
			} catch (IOException exception) {
				exception.printStackTrace();
			}
		}
		return bitmap;
	}
}
